package com.axc.gallery2.edit.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ZoomControls;

import com.axc.gallery2.R;
import com.axc.gallery2.edit.EditActivity;
import com.axc.gallery2.edit.ToReleaseBitmap;
import com.axc.gallery2.edit.effective.ToZoom;
import com.axc.gallery2.edit.effective.ToZoomImage;
import com.axc.gallery2.edit.effective.ToZoomPercentage;

public class SwitchImage extends Activity implements OnTouchListener {
	ImageView imageView;
	public static Bitmap bitmapMain;// 画布内容
	public static Bitmap bitmapSave;

	Bitmap bitmapTop1;
	Bitmap bitmapTop2;
	Bitmap bitmapTop3;
	Bitmap bitmapTop4;
	Bitmap bitmapTop5;
	Canvas canvas;// 画布
	Paint paint = null;
	LinearLayout imagesMenu;

	LinearLayout layoutMenu;
	LinearLayout bottomMenu;

	List<ImageView> imageViews = new ArrayList<ImageView>();

	// 保存 、重设、取消、 按钮
	private Button btnSave;
	private Button btnReset;
	private Button btnCancle;
	Button[] btnOptionMenu = { btnSave, btnReset, btnCancle };
	int[] OptionMenuid = { R.id.btn_Save, R.id.btn_Reset, R.id.btn_Cancle };

	// X Y 坐标
	float x, y = 0;

	// 图一 位置（ x,y）
	public float ix = 12;
	public float iy = 12;
	// 图二 位置（ x,y）
	public float ix2 = 12;
	public float iy2 = 12;
	// 图三 位置（ x,y）
	public float ix3 = 12;
	public float iy3 = 12;
	// 图四 位置（ x,y）
	public float ix4 = 12;
	public float iy4 = 12;
	// 图五 位置（ x,y）
	public float ix5 = 12;
	public float iy5 = 12;

	// 获取LinearLayout的长宽
	int LinearWidth = 110;
	int LinearHeight = 110;

	Bitmap drawimBitmap;
	static Bitmap drawImageBitmap;

	// 放大与缩小
	ZoomControls zoom;
	float zoomPercentage = 1.0f;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_decorate);
		// 获取菜单
		layoutMenu = (LinearLayout) findViewById(R.id.switch_layoutMenu);
		bottomMenu = (LinearLayout) findViewById(R.id.bottomMenu);
		imagesMenu = (LinearLayout) findViewById(R.id.imgsMenu);

		// Imagevivews
		for (int i = 0; i < imagesMenu.getChildCount(); i++) {
			View view = imagesMenu.getChildAt(i);
			if (view instanceof ImageView) {
				ImageView v = (ImageView) imagesMenu.getChildAt(i);
				v.setOnLongClickListener(switchListener);
				imageViews.add(v);
			}
		}

		imageView = (ImageView) findViewById(R.id.drawImageview);
		// 获取传进来的图片
		bitmapMain = new ToReleaseBitmap().RelBmp(EditActivity.imageFileNames);//((BitmapDrawable) getResources().getDrawable(R.drawable.girl)).getBitmap();
		// 设置ImageView
		imageView.setImageBitmap(bitmapMain);

		// 图片
		bitmapTop1 = ((BitmapDrawable) getResources().getDrawable(
				R.drawable.ic_launcher)).getBitmap();
		bitmapTop2 = ((BitmapDrawable) getResources().getDrawable(
				android.R.drawable.ic_input_add)).getBitmap();
		bitmapTop3 = ((BitmapDrawable) getResources().getDrawable(
				android.R.drawable.ic_search_category_default)).getBitmap();
		bitmapTop4 = ((BitmapDrawable) getResources().getDrawable(
				android.R.drawable.ic_lock_idle_alarm)).getBitmap();
		bitmapTop5 = ((BitmapDrawable) getResources().getDrawable(
				android.R.drawable.ic_notification_clear_all)).getBitmap();
		// 逻辑上的两个图片交替使用
		bitmapSave = bitmapMain;

		// 屏幕为360*240时
		int width = 360;
		int height = 480;

		Log.w("w", String.valueOf(getWindowManager().getDefaultDisplay()
				.getWidth()));
		Log.w("w", String.valueOf(getWindowManager().getDefaultDisplay()
				.getHeight()));
		// 设置画布大小
		// Bitmap drawImageBitmap =
		// Bitmap.createBitmap((int)(bitmapMain.getWidth()),
		// (int)(bitmapMain.getHeight()), Config.RGB_565);
		drawImageBitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
		drawimBitmap = new ToZoom().ProcessImage(bitmapMain, width, height);// (int)(bitmapMain.getWidth()*0.8),
		// (int)(bitmapMain.getHeight()*0.8));

		canvas = new Canvas(drawImageBitmap);
		canvas.clipRect(0, 0, 360, 480);
		bitmapMain = new ToZoomImage().ProcessImage(bitmapMain, width, height);
		Paint paint = new Paint();
		paint.setColor(Color.YELLOW);
		// canvas.drawRect(new Rect(0, 0, bitmapMain.getWidth(),
		// bitmapMain.getHeight()),paint );
		// 设置为画布然后每次在画布画，通过Invidial()方法 重画
		imageView.setImageBitmap(drawImageBitmap);
		imageView.setOnTouchListener(this);

		// 起始就画一幅图 （显示原图）
		drawImage(canvas, bitmapMain, 0, 0);

		SetButton(btnOptionMenu, OptionMenuid, srcListener);

		// 缩放 放大按钮工具
		zoom = (ZoomControls) findViewById(R.id.zoomControls1);
		// 放大
		zoom.setOnZoomInClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				zoomPercentage += 0.1;
				Log.w("in", "in");
				canvas = new Canvas(drawImageBitmap);
				bitmapSave = new ToZoomPercentage().ProcessImage(bitmapMain,
						zoomPercentage);
				imageView.setImageBitmap(bitmapSave);
				// 起始就画一幅图 （显示原图）
				drawImage(canvas, bitmapSave, 0, 0);
				bitmapSave = null;

			}
		});

		// 缩小
		zoom.setOnZoomOutClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (zoomPercentage > 0.3) {
					zoom.setEnabled(false);
					zoomPercentage -= 0.1;
					Log.w("in", "out");
					//canvas = new Canvas(drawImageBitmap);
					bitmapSave = new ToZoomPercentage().ProcessImage(
							drawImageBitmap, zoomPercentage);
					imageView.setImageBitmap(bitmapSave);
					// 起始就画一幅图 （显示原图）
					drawImage(canvas, bitmapSave, 0, 0);
				} else {
					zoom.setEnabled(false);
				}
			}

		});

	}

	// 搜索按钮及其ID
	public void SetButton(Button[] btns, int[] ids, OnClickListener listener) {
		for (int i = 0; i < btns.length; i++) {
			btns[i] = setBtnAct(ids[i], listener);
		}
	}

	// 自动监听事件
	public Button setBtnAct(int Rid, OnClickListener listener) {
		Button btnView = (Button) findViewById(Rid);
		btnView.setOnClickListener(listener);
		return btnView;
	}

	// 监听按钮：保存 取消 重设
	private OnClickListener srcListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_Save:
				Save(bitmapSave);
				// Toast.makeText(EditActivity.this, "保存成功", Toast.LENGTH_LONG);
				Log.w("T", "Success");
				break;
			case R.id.btn_Reset:
				canvas.drawColor(Color.BLUE);
				canvas.drawBitmap(bitmapMain, 0, 0, paint);
				imageView.invalidate();
				break;
			case R.id.btn_Cancle:

				break;
			default:
				break;
			}

		}
	};

	public void Save(Bitmap bitmap) {
		// TODO Auto-generated method stub
		//Isave saveImg = new SaveImage();
		//saveImg.Save(bitmap);
	}

	/*
	 * @Override public boolean onTouchEvent(MotionEvent event) { // TODO
	 * Auto-generated method stub int action=event.getAction(); switch (action)
	 * { case MotionEvent.ACTION_MOVE: Log.w("M", "moving");
	 * x=(int)event.getX(); y=(int)event.getY(); Log.w("M", String.valueOf(x));
	 * Log.w("M", String.valueOf(y)); //开始底部画板 //drawImage(canvas, bitmapMain,
	 * x, y); //imageView.invalidate(); canvas.drawColor(Color.GRAY);
	 * drawImage(canvas, bitmapMain, 0, 0); drawImage(canvas, bitmapTop, x, y);
	 * canvas.drawLine(0, 0, 199, 199, paint); imageView.invalidate(); break;
	 * 
	 * default: break; } return super.onTouchEvent(event); }
	 */
	// 重新描画顶图的位置
	public static void drawImage(Canvas canvas, Bitmap bitmap, float x, float y) {
		// 绘制图像 将bitmap对象显示在坐标 x,y上
		canvas.drawBitmap(bitmap, x, y, null);
		bitmapSave = drawImageBitmap;
	}

	Boolean boolean1 = true;
	Boolean boolMenu = true;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:

			Log.w("T", "True");
			x = event.getX();
			y = event.getY();
			Log.w("M", "x:" + String.valueOf(x));
			Log.w("M", "y:" + String.valueOf(y));
			// 根据不同图片大小
			if (x - ix <= 20 && x - ix >= -20 && y - iy <= 20 && y - iy >= -20) {
				boolean1 = true;
				boolMenu = false;
			} else {
				boolean1 = false;
				boolMenu = true;
			}
			if (boolMenu) {
				if (layoutMenu.getVisibility() == View.INVISIBLE) {
					layoutMenu.setVisibility(View.VISIBLE);
					bottomMenu.setVisibility(View.VISIBLE);
				} else {
					layoutMenu.setVisibility(View.INVISIBLE);
					bottomMenu.setVisibility(View.INVISIBLE);
				}
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (boolean1) {
				Log.w("M", "moving");
				x = event.getX();
				y = event.getY();
				// canvas.drawColor(Color.GRAY);
				drawImage(canvas, bitmapSave, 0, 0);
				//drawImage(canvas, bitmapMain, 0, 0);
				drawImage(canvas, bitmapTop1, x, y);
				imageView.invalidate();
				// imageView.postInvalidate();
			}
			break;
		case MotionEvent.ACTION_UP:
			ix = event.getX();
			iy = event.getY();
			Log.w("M", "ix:" + String.valueOf(ix));
			Log.w("M", "iy:" + String.valueOf(iy));
			break;

		default:
			break;
		}
		return true;
	}

	// new 引入
	// 按键按下事件
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 左方向键
		if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			if (x > 0) {
				x -= 4;
			}
		}
		// 右方向键
		else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			if (x + bitmapTop1.getWidth() < 320) {
				x += 4;
			}
		}
		// 上方向键
		else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			if (y > 0) {
				// miDTY + mBitDestTop.getHeight() < 450
				y -= 4;
			}
		}
		// 下方向键
		else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			if (y < 322) {
				y += 4;
			}
			y += 4;
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.finish();
		}
		drawImage(canvas, bitmapMain, 0, 0);
		drawImage(canvas, bitmapTop1, x, y);
		imageView.invalidate();
		return true;
	}

	OnLongClickListener switchListener = new OnLongClickListener() {

		@Override
		public boolean onLongClick(View v) {
			// TODO Auto-generated method stub
			Log.w("my", "onlongclick");
			switch (v.getId()) {
			case R.id.addImage1:
				drawImage(canvas, bitmapTop1, x, y);
				break;
			case R.id.addImage2:
				drawImage(canvas, bitmapTop2, x, y);
				break;
			case R.id.addImage3:
				drawImage(canvas, bitmapTop3, x, y);
				break;
			case R.id.addImage4:
				drawImage(canvas, bitmapTop4, x, y);
				break;
			case R.id.addImage5:
				drawImage(canvas, bitmapTop5, x, y);
				break;
			default:
				break;
			}
			return true;
		}
	};

}
